<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2018/12/29
 * Time: 9:17
 */
namespace app\admin\controller;
use think\Db;
class Factory extends Base {

    /*****
     * @return mixed|\think\response\Json
     * 首页
     */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];

            if(isset($f_area)&&$f_area != ""){
                $map['a.f_area'] = $f_area;
            }
            if(isset($f_industry_id)&&$f_industry_id != ""){
                $map['a.f_industry_id'] = $f_industry_id;
            }
            if(isset($key)&&$key!="")
            {
                $map['a.factory_name|up.p_username'] = ['like',"%" . $key . "%"];
            }
            //时间筛选
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['a.f_create_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['a.f_create_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['a.f_create_time'] = ['between time',[$start,$end]];
            }
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_factory')->alias('a')
                ->join('up_province up','up.p_id = a.f_id','left')
                ->join('up_industry ui','ui.industry_id = a.f_industry_id','left')
                ->join('up_area ua','ua.district_id = a.f_area','left')
                ->where($map)
                ->count();//计算总页面
            $list = Db::table('up_factory')
                ->alias ('a')
                ->join('up_province up','up.p_id = a.f_id','left')
                ->join('up_industry ui','ui.industry_id = a.f_industry_id','left')
                ->join('up_area ua','ua.district_id = a.f_area','left')
                ->page($page, $limit)
                ->where($map)
                ->field('a.*,up.p_username,ui.industry_name,ua.district')
                ->select();
            foreach ($list as $key=>&$vo){
                $where1['m_machine'] = array('NEQ','NULL');
                $where1['m_factory_id'] = $vo['factory_id'];
                $num = Db::table('up_factory_member')->where($where1)->count();
                $vo['num'] = $num;
            }
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        $industry = Db::table('up_industry')->select();
        $data = Db::table('up_area')->where('level',1)->select();
        $this->assign('data',$data);
        $this->assign('industry',$industry);
        if(session('?f_id')){
            session('f_id',null);
        }
        return $this->fetch();
    }


    /***
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $id = input('param.id');
        $where1['m_factory_id'] = $id;
        $where1['m_machine'] =array('neq','null');
        $info = Db::table('up_factory_member')->where($where1)->find();
        if($info['m_id']!=''){
            return json(['code' => 100, 'data' => '', 'msg' => '当前账号下有终端信息，不允许删除']);
        }else{
           $res =  Db::table('up_factory')->where('factory_id',$id)->delete();
            if($info['m_id']!=''){//该厂家下面有用户信息,删除
                Db::table('up_factory_member')->where('m_factory_id',$id)->delete();
            }
            $info1 = Db::table('up_work_order')->where('w_fac_id',$id)->find();
            if($info1['w_id']!=''){//该厂家下面有工单删除
                Db::table('up_work_order')->where('w_fac_id',$id)->delete();
            }
            if($res){
                return json(['code' => 200, 'data' => '', 'msg' => '删除成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '删除失败']);
            }
        }
    }

    /*****
     * @return mixed
     * 添加
     */
    public function add(){
        if(request()->isPost()){
            $len = mb_strlen(input('param.factory_name'),'utf8');
            if($len>30){
                return json(['code' => 100, 'data' => '', 'msg' => '不能超过30个字符']);
            }
            $res1 = Db::table('up_factory')->where('factory_name',input('param.factory_name'))->find();
            if ($res1['factory_id']!=''){
                return json(['code' => 100, 'data' => '', 'msg' => '不能重复添加厂家名称']);
            }
            if(mb_strlen(input('param.factory_password'),'UTF8')>16){
                return json(['code' => 100, 'data' => '', 'msg' => '密码最多不能超过16位']);
            }
//            $area = input('param.f_area');
            $data['factory_account'] =$this->randAccount();
            $data['factory_name'] = input('param.factory_name');
            $data['factory_password'] = md5(input('param.factory_password'));
            $data['factory_pass'] = input('param.factory_password');
            $data['f_id'] = input('param.f_id');
            $data['f_area'] = input('param.f_area');
            $data['f_industry_id'] = input('param.f_industry_id');
            $data['f_create_time'] = date('Y-m-d H:i:s',time());
            $res =  Db::table('up_factory')->insertGetId($data);
            if($res){
                return json(['code' => 200, 'data' =>"", 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '添加失败']);
            }
        }
        $data = Db::table('up_area')->where('level',1)->select();
        $data1 = Db::table('up_industry')->select();
        $data2 = Db::table('up_province')->select();
        $this->assign('data',$data);
        $this->assign('data1',$data1);
        $this->assign('data2',$data2);
        return $this->fetch();
    }

    public function randAccount(){
        $str = 'C'.rand(10000000,99999999);
        $res1 = Db::table('up_factory')->where('factory_account',$str)->find();
        if($res1['factory_id']!=''){
            $this->randAccount();
        }
        return $str;
    }

    /**
     * @return mixed
     * 编辑
     */
    public function update(){
        if(request()->isPost()){
            $id = input('param.id');
            $len = mb_strlen(input('param.factory_name'),'utf8');
            if($len>30){
                return json(['code' => 100, 'data' => '', 'msg' => '不能超过30个字符']);
            }
            $where['factory_name'] = input('param.factory_name');
            $where['factory_id'] = array('NEQ',$id);
            $res1 = Db::table('up_factory')->where($where)->find();
            if ($res1['factory_id']!=''){
                return json(['code' => 100, 'data' => '', 'msg' => '不能重复添加厂家名称']);
            }
            if(mb_strlen(input('param.factory_password'),'UTF8')>16){
                return json(['code' => 100, 'data' => '', 'msg' => '密码最多不能超过16位']);
            }
            $data4['factory_name'] = input('param.factory_name');
            $data4['factory_password'] = md5(input('param.factory_password'));
            $data4['factory_pass'] = input('param.factory_password');
//            $data4['f_id'] = input('param.f_id');
//            $data4['f_area'] = input('param.f_area');
//            $data4['f_industry_id'] = input('param.f_industry_id');
            $res4 =  Db::table('up_factory')->where('factory_id',$id)->update($data4);
            if($res4){
                return json(['code' => 200, 'data' =>"", 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '编辑失败']);
            }
        }
        $id = input('param.id');
        $res = Db::table('up_factory')->where('factory_id',$id)->find();
        $data = Db::table('up_area')->where('level',1)->select();
        $data1 = Db::table('up_industry')->select();
        $data2 = Db::table('up_province')->select();
        $this->assign('data',$data);
        $this->assign('data1',$data1);
        $this->assign('data2',$data2);
        $this->assign('res',$res);
        return $this->fetch();
    }


    public function detail(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
//            if(isset($key)&&$key!="")
//            {
//                $map['uf.factory_name|up.p_username'] = ['like',"%" . $key . "%"];
//            }
//            if(isset($key)&&$key!="")
//            {
//                $map['uf.factory_name|up.p_username'] = ['like',"%" . $key . "%"];
//            }
//            //厂家名称
//            if(isset($fid)&&$fid!="")
//            {
//                $map['uf.factory_id'] = $fid;
//            }
//            //省级管理员名称
//            if(isset($pid)&&$pid!="")
//            {
//                $map['up.p_id'] = $pid;
//            }
//            //时间筛选
//            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
//            {
//                $map['a.m_create_time'] = ['>= time',$start];
//            }
//            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
//            {
//                $map['a.m_create_time'] = ['<= time',$end];
//            }
//            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
//            {
//                $map['a.m_create_time'] = ['between time',[$start,$end]];
//            }
            $map['a.m_factory_id'] = input('param.id');
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_factory_member')->alias('a')
                ->join('up_factory uf','uf.factory_id = a.m_factory_id','left')
                ->join('up_province up','up.p_id = uf.f_id','left')
                ->where($map)
                ->count();//计算总页面
            $list = Db::table('up_factory_member')
                ->alias ('a')
                ->join('up_factory uf','uf.factory_id = a.m_factory_id','left')
                ->join('up_province up','up.p_id = uf.f_id','left')
                ->page($page, $limit)
                ->where($map)
                ->field('a.*,up.p_username,uf.factory_name')
                ->select();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        if(!session('?f_id')){
            session('f_id',input('id'));
        }
        return $this->fetch();
    }

    /****
     * 删除
     */
    public function dell(){
        $id = input('param.id');
        $res = Db::table('up_factory_member')->where('m_id',$id)->delete();
        if($res){
            return json(['code' => 200, 'data' => '', 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'data' => '', 'msg' => '删除失败']);
        }
    }
}